package com.ymk.cmd;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ymk.base.cmd.ACmd;
import com.ymk.base.cmd.CmdSign;
import com.ymk.base.exception.MallException;
import com.ymk.base.service.WxService;
import com.ymk.base.utlis.ParamsBuilder;
import com.ymk.bo.user.UserInfoBO;
import com.ymk.bo.work.WorkApplyBO;
import com.ymk.core.code.cmd.GoodsCmdCode;
import com.ymk.core.code.cmd.UserCmdCode;
import com.ymk.core.code.cmd.WorkApplyCmdCode;
import com.ymk.core.constants.SysConstants;
import com.ymk.core.constants.WxConstants;
import com.ymk.service.IWorkApplyService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Service
public class WorkApplyPostCmd extends ACmd {

    @Override
    public CmdSign executeSop(HttpServletRequest request, CmdSign sign) throws Exception {
        CmdSign respSign = new CmdSign(sign);
        WorkApplyBO reqData = super.getData(sign, WorkApplyBO.class);

        this.setCreatedData(sign,reqData);
        this.WorkApplyService.post(reqData);

        //查询管理员
        ParamsBuilder paramsBuilder = ParamsBuilder.newBuild()
                .addParam("role",2);
        CmdSign userCmd = CmdSign.Builder.newBuild(sign)
                .setSource(paramsBuilder.build())
                .setCmdCode(UserCmdCode.USER_LIST)
                .setScope(SysConstants.CMD_SERVER_SCOPE)
                .build();
        List<UserInfoBO> userInfoBOS = this.scheduler.schedule(request, userCmd, UserInfoBO.class, true);

        // 新增地区招商后 推送消息通知给管理员
        if(!userInfoBOS.isEmpty()) {
            wxService.sendMessage(reqData, userInfoBOS);
        }

        return respSign;
    }





    @Override
    public String getCmdCode() {
        return WorkApplyCmdCode.WORK_APPLY_POST;
    }

    @Override
    public boolean anon(CmdSign sign) {
        return true;
    }
    @Autowired
    private IWorkApplyService WorkApplyService;

    @Autowired
    private WxService wxService;
}